<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ProfileVerify;
use app\admin\model\User;
use App\Http\Controllers\Controller;
use App\Model\ProfileVerifyLog;
use App\Model\SettingUserQuestion;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Util\ComUtil;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use App\Model\GuildManage;
use App\Model\Guild\GuildHost;
use App\Model\Guild\GuildHostManage;

/**
 * 真人头像
 */
class RealAvatarController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('真人头像认证');
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {

        $grid = new Grid(new ProfileVerifyLog());
        $where = [
            ['content_type', ProfileVerifyLog::TYPE_REAL_AVATAR],
        ];
        $adminId = Admin::user()->id;

        if($adminId == 50 || $adminId == 51 ){
            $ghId= GuildManage::getGuidIdByGuildUserId($adminId);

            $userIds = GuildHostManage::whereIn('guild_manage_id',$ghId)->pluck('host_user_id')->toArray();
//            $userIds = UserProfile::whereIn('invite_code',$ctHostId)->pluck('user_id');
            if(!$userIds) $userIds = [];

            $grid->model()->where($where)->whereIn('user_id',$userIds)->orderBy('id', 'desc');
        }else{
            $grid->model()->where($where)->orderBy('id', 'desc');
        }

        $grid->id('记录ID');
        $grid->avatar('认证头像')->display(function ($data) {
            if (!$this->content) return '-';
            $info = json_decode($this->content, true);
            if (!isset($info['avatar'])) return '-';
            return ComUtil::buidlImgaeHtml($info['avatar']);
        });
        $grid->content('阿里云采集头像')->display(function ($data) {
            if (!$this->content) return '-';
            $info = json_decode($this->content, true);
            if (!isset($info['real_avatar'])) return '-';
            return ComUtil::buidlImgaeHtml($info['real_avatar']);
        });
        $grid->content_type('类型')->display(function ($data) {
            return ProfileVerifyLog::TYPE[$data] ?? '未知类型';
        });
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->gender('性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->userProfile()->nick_name('用户昵称');
        $grid->created_at('认证时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->status('状态')->display(function ($data) {
            return ProfileVerifyLog::STATUS[$data];
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();

            // $filter->column(1 / 3, function ($filter) {
            //     $filter->equal('status', '状态')->select(ProfileVerifyLog::STATUS);
            // });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });

            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.gender', '性别')->select([1 => '男', 0 => '女']);;
            });

            $filter->column(1 / 3, function ($filter) {
                $filter->equal('status', '审核状态')->select(ProfileVerifyLog::STATUS);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->between('created_at', '认证时间')->datetime();
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new ProfileVerify($actions->getKey(), $actions->row, 'real_avatar/verify'));
        });
        return $grid;
    }

    function verify()
    {
        $id = request('id');
        $status = intval(request('status'));
        $record = ProfileVerifyLog::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        //修改状态
        $record->update(['status' => $status]);
        //审核不通过
        $userId = $record->user_id;
        if (ProfileVerifyLog::STATUS_BAD == $status) {
            switch ($record->content_type) {
                case ProfileVerifyLog::TYPE_REAL_AVATAR:

                    UserProfile::where('user_id', $userId)->update([
                        'avatar_vrfy' => 0,
                        'role' => UserProfile::ROLE_USER
                    ]);

                    UserProfilePersonal::where('user_id', $userId)->update([
                        'face_vry_pic' => ''
                    ]);

                    \Log::info(__METHOD__ . "==userId" . $userId . "==去掉真人认证" );

                    break;
            }
        }

        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
